package com.joplus.housekeeper.ui.view;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.joplus.housekeeper.R;
import com.joplus.housekeeper.adapter.EventSelectHorizontalAdapter;
import com.joplus.housekeeper.adapter.TicketsAdapter;
import com.joplus.housekeeper.bean.EventSelectDetail;
import com.joplus.housekeeper.bean.TicketOrderDetail;
import com.joplus.housekeeper.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 长期活动的票布局
 * author Administrator
 * time 2016/6/29 14:34
 */
public class LongActivitiesTicketLayout extends LinearLayout implements View.OnClickListener, TicketsAdapter.OnPriceChanged, AdapterView.OnItemClickListener {

    @Override
    public void onPrice(double price) {
        //这里处理用户点击票型后返回的总价
        if (monSecetedTimeClicked != null) {
            monSecetedTimeClicked.onGetPrice(price);
        }

    }

    @Override
    public void onSelectedTicket(List<TicketOrderDetail> ts) {
        //这里处理用户选择票型的事件后返回的数量不为0的票
        if (monSecetedTimeClicked != null) {
            monSecetedTimeClicked.onGetData(ts);
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //这里处理recyleView 的点击回调

        //每一次点击都把价格设置为0
        if (monSecetedTimeClicked != null) {

            monSecetedTimeClicked.onGetPrice(-1);

        }

        if (monSecetedTimeClicked != null) {
            monSecetedTimeClicked.onClick(EventType.PERIOD, position);
        }
        RefreshPriceListView1(dealPriceList(eventSelectDetail.getDataBean().getEventPayDateilSessionDTOs()).get(position), eventSelectDetail.getDataBean().isDisney());
        eventSelectHorizontalAdapter.notifyData(eventSelectDetail.getDataBean().getEventPayDateilSessionDTOs(), position);
    }

    public static enum EventType {
        LONGTIME, PERIOD
    }

    TextView tvTime;
    ListView ticketsListView;
    TicketsAdapter ticketsAdapter;
    RecyclerView periodEventsView;//场次活动横向滑动View

    onSecetedTimeClicked monSecetedTimeClicked;

    EventSelectDetail eventSelectDetail;
    View view;
    Context context;
    LayoutInflater layoutInflater;
    EventSelectHorizontalAdapter eventSelectHorizontalAdapter;

    public LongActivitiesTicketLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.layout_long_activities_ticket, this);
    }

    public LongActivitiesTicketLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, null);
        this.context = context;
    }

    public LongActivitiesTicketLayout(Context context) {
        super(context);
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.layout_long_activities_ticket, this);
    }

    public void setDate(String memId, EventSelectDetail d) {

        //已经加载过布局,防止多次加载
        if (eventSelectDetail != null) {
            return;
        }
        eventSelectDetail = d;

        //type 为1是长期活动，0是场次活动
        if (d.getDataBean().getType() == 1) {
            CreatLongTimeEnentsLayout(context, memId, d);
        } else if (d.getDataBean().getType() == 0) {
            CreatPeriodEnentsLayout(context, memId, d);

        }

        RefreshPriceListView1(dealPriceList(d.getDataBean().getEventPayDateilSessionDTOs()).get(0), d.getDataBean().isDisney());

    }

    public void get() {
        ticketsAdapter.setOnPriceChanged(this);

    }

    private List<List<EventSelectDetail.DataBean.EventPayDateilSessionDTOsBean.TypeListBean>> dealPriceList(List<EventSelectDetail.DataBean.EventPayDateilSessionDTOsBean> beans) {

        List<List<EventSelectDetail.DataBean.EventPayDateilSessionDTOsBean.TypeListBean>> pricelist = new ArrayList<>();
        for (EventSelectDetail.DataBean.EventPayDateilSessionDTOsBean bean : beans) {
            pricelist.add(bean.getTypeList());
        }
        return pricelist;

    }


    private void CreatLongTimeEnentsLayout(Context context, String memId, EventSelectDetail d) {

        LinearLayout layout = (LinearLayout) findViewById(R.id.ll_selcect_period_or_time);
        View view2 = layoutInflater.inflate(R.layout.layout_event_data_select, null);
        layout.addView(view2);

        tvTime = (TextView) findViewById(R.id.textView3);
        tvTime.setOnClickListener(this);
        ticketsListView = (ListView) findViewById(R.id.lv_ticketsSelect);
        ticketsAdapter = new TicketsAdapter(context, memId);
        ticketsListView.setAdapter(ticketsAdapter);
        ticketsAdapter.setOnPriceChanged(this);
        findViewById(R.id.rl_timelayout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (monSecetedTimeClicked != null) {
                    monSecetedTimeClicked.onClick(EventType.LONGTIME, 0);
                }
            }
        });
    }


    private void CreatPeriodEnentsLayout(final Context context, String memId, EventSelectDetail d) {


        eventSelectHorizontalAdapter = new EventSelectHorizontalAdapter(d.getDataBean().getEventPayDateilSessionDTOs());


        //
        LinearLayout layout = (LinearLayout) findViewById(R.id.ll_selcect_period_or_time);

        View view = layoutInflater.inflate(R.layout.layout_event_period_select, layout, false);


        layout.addView(view);
        //periodEventsView.addItemDecoration(new InsetDecoration(context));
        periodEventsView = (RecyclerView) findViewById(R.id.rcv_period_event);
        periodEventsView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        periodEventsView.addItemDecoration(new InsetDecoration(context));

        ticketsListView = (ListView) findViewById(R.id.lv_ticketsSelect);
        ticketsAdapter = new TicketsAdapter(context, memId);
        ticketsListView.setAdapter(ticketsAdapter);
        ticketsAdapter.setOnPriceChanged(this);
        periodEventsView.setAdapter(eventSelectHorizontalAdapter);
        eventSelectHorizontalAdapter.setOnItemClickListener(this);
        eventSelectHorizontalAdapter.notifyData(d.getDataBean().getEventPayDateilSessionDTOs(), 0);

    }

    /**
     * 设置长期活动的显示日期
     *
     * @param date
     */
    public void setEventDate(String date) {
        if (tvTime != null) {
            StringUtils.setText(tvTime, date);
        }
    }

    /**
     * 设置价格列表的memberId，用来筛选用户等级对应的价格
     *
     * @param memberId
     */
    public void setPriceListAdapterMemberId(String memberId) {
        ticketsAdapter.setMemberId(memberId);
    }

    /**
     * ；刷新价格数据list，高度根据内部item数量自动设定
     *
     * @param typeList
     */
    private void RefreshPriceListView1(List<EventSelectDetail.DataBean.EventPayDateilSessionDTOsBean.TypeListBean> typeList, boolean isdiseny) {


        ticketsAdapter.notifyDataSetChanged(typeList, isdiseny);
        autoSetListHeight(ticketsAdapter, ticketsListView);
    }


    /**
     * ；刷新价格数据list，高度根据内部item数量自动设定
     *
     * @param beans
     */
    public void RefreshPriceListView(List<EventSelectDetail.DataBean.EventPayDateilSessionDTOsBean> beans, boolean isdiseny) {

        ticketsAdapter.notifyDataSetChanged(dealPriceList(beans).get(0), isdiseny);
        autoSetListHeight(ticketsAdapter, ticketsListView);
    }


    /**
     * 重设listview高度
     *
     * @param adapter
     * @param listView
     */
    private void autoSetListHeight(BaseAdapter adapter, ListView listView) {
        int totalHeight = 0;

        for (int i = 0; i < adapter.getCount(); i++) {
            View listItem = adapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listView.getCount() - 1));
        ((MarginLayoutParams) params).setMargins(10, 10, 10, 10);
        listView.setLayoutParams(params);
    }

    /**
     * 设置时间点击接口
     *
     * @param tc
     */
    public void setOnSecetedTimeClicked(onSecetedTimeClicked tc) {
        monSecetedTimeClicked = tc;
    }

    public interface onSecetedTimeClicked {
        //这里的position 是留给横向滑动RecyleView使用的，
        public void onClick(EventType type, int postion);

        public void onGetData(List<TicketOrderDetail> selectedTicketsDetail);

        public void onGetPrice(double price);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.textView3:
                //点击长期活动的的回调
                monSecetedTimeClicked.onClick(EventType.LONGTIME, 0);
                break;
        }

    }


}
